package controller

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"os"
	"products/global"
	"products/types"
	"strconv"
)

// 商品列表展示
func GoodsList(r *gin.Context) {

	//分页
	page1 := r.PostForm("page")
	pageSize1 := r.PostForm("pageSize")
	status := r.PostForm("status")

	page, _ := strconv.Atoi(page1)
	pageSize, _ := strconv.Atoi(pageSize1)
	GoodsStatus, _ := strconv.Atoi(status)

	//判断是否上架

	var goodFind types.Goods

	find, err := goodFind.Find(GoodsStatus)
	if err != nil {
		return
	}

	global.Db.Table("goods").Limit(pageSize).Offset((page - 1) * pageSize).Find(&find)

	r.JSON(http.StatusOK, gin.H{"code": "0", "msg": "数据展示", "find": find})

}

func GoodsInter(r *gin.Context) {
	goods_name := r.PostForm("goods_name")
	goods_title := r.PostForm("goods_title")
	goods_text := r.PostForm("goods_text")
	goods_img := r.PostForm("goods_img")
	goods_url := r.PostForm("goods_url")
	Video_id := r.PostForm("video_id")
	Goods_introduce := r.PostForm("goods_introduce")
	Goods_price := r.PostForm("goods_price")
	Sub_price := r.PostForm("sub_price")
	Goods_stock := r.PostForm("goods_stock")
	Goods_num := r.PostForm("goods_num")
	Goods_status := r.PostForm("goods_status")

	video_id, _ := strconv.Atoi(Video_id)
	goods_price, _ := strconv.Atoi(Goods_price)
	sub_price, _ := strconv.Atoi(Sub_price)
	goods_stock, _ := strconv.Atoi(Goods_stock)
	goods_num, _ := strconv.Atoi(Goods_num)
	goods_status, _ := strconv.Atoi(Goods_status)

	goods := types.Goods{
		GoodsName:      goods_name,
		GoodsTitle:     goods_title,
		GoodsText:      goods_text,
		GoodsImg:       goods_img,
		GoodsUrl:       goods_url,
		VideoId:        video_id,
		GoodsIntroduce: Goods_introduce,
		GoodsPrice:     goods_price,
		SubPrice:       sub_price,
		GoodsStock:     goods_stock,
		GoodsNum:       goods_num,
		GoodsStatus:    goods_status,
	}

	global.Db.Table("goods").Save(&goods)

	r.JSON(http.StatusOK, gin.H{"goods": goods, "id": goods.ID})

}

func Highlight(text string) {

	err := os.Link(text, "")
	if err != nil {
		return
	}

}

// 3．商品搜索：商品搜索后结果要求高亮显示搜索结果，搜索后可以进行商品分类筛选。
func GoodsSearch(r *gin.Context) {

	order := r.PostForm("orderType")
	str := r.PostForm("textStr")

	orderType, err := strconv.Atoi(order)
	if err != nil {
		return
	}

	var goodsList types.Goods

	search, err := goodsList.Search(orderType, str)
	if err != nil {
		return
	}

	//es 搜索
	var goods types.Goods

	marshal, err := json.Marshal(goods)
	if err != nil {
		return
	}

	request, err := http.NewRequest("POST", "http://113.44.72.167:5601/"+"goods/_seach", bytes.NewBuffer(marshal))
	if err != nil {
		return
	}

	request.Header.Set("Content-Type", "application/json")

	client := &http.Client{}

	response, err := client.Do(request)
	if err != nil {
		return
	}

	defer response.Body.Close()

	body, _ := ioutil.ReadAll(response.Body)

	bodyStr := string(body)

	fmt.Println(bodyStr)

	r.JSON(http.StatusOK, gin.H{"search": search, "body": bodyStr})
}

func GoodsAdd(r *gin.Context) {
	/*
		POST /video/_doc/3
		{
		  "video_name": "矿泉水",
		  "video_path": "哈哈哈",
			"video_url": "httpsdnsigoqoe",
			"video_sort": 537,
		  "video_status":984
		}
	*/

	goods_name := r.PostForm("goods_name")
	goods_title := r.PostForm("goods_title")
	goods_text := r.PostForm("goods_text")
	goods_img := r.PostForm("goods_img")
	goods_url := r.PostForm("goods_url")
	Video_id := r.PostForm("video_id")
	Goods_introduce := r.PostForm("goods_introduce")
	Goods_price := r.PostForm("goods_price")
	Sub_price := r.PostForm("sub_price")
	Goods_stock := r.PostForm("goods_stock")
	Goods_num := r.PostForm("goods_num")
	Goods_status := r.PostForm("goods_status")

	video_id, _ := strconv.Atoi(Video_id)
	goods_price, _ := strconv.Atoi(Goods_price)
	sub_price, _ := strconv.Atoi(Sub_price)
	goods_stock, _ := strconv.Atoi(Goods_stock)
	goods_num, _ := strconv.Atoi(Goods_num)
	goods_status, _ := strconv.Atoi(Goods_status)

	goods := types.Goods{
		GoodsName:      goods_name,
		GoodsTitle:     goods_title,
		GoodsText:      goods_text,
		GoodsImg:       goods_img,
		GoodsUrl:       goods_url,
		VideoId:        video_id,
		GoodsIntroduce: Goods_introduce,
		GoodsPrice:     goods_price,
		SubPrice:       sub_price,
		GoodsStock:     goods_stock,
		GoodsNum:       goods_num,
		GoodsStatus:    goods_status,
	}

	global.Db.Table("goods").Create(&goods)

	marshal, err := json.Marshal(goods)
	if err != nil {
		return
	}

	request, err := http.NewRequest("POST", "http://113.44.72.167:5601/"+"goods/_doc", bytes.NewBuffer(marshal))
	if err != nil {
		return
	}

	request.Header.Set("Content-Type", "application/json")

	client := &http.Client{}

	response, err := client.Do(request)
	if err != nil {
		return
	}

	defer response.Body.Close()

	body, _ := ioutil.ReadAll(response.Body)

	bodyStr := string(body)

	fmt.Println(bodyStr)

	r.JSON(http.StatusOK, gin.H{"goods": goods, "id": goods.ID, "body": bodyStr})
}

func GetGoodsList(r *gin.Context) {
	var goods []types.Goods
	global.Db.Table("goods").Find(&goods)

	r.JSON(http.StatusOK, gin.H{"goods": goods})
}

func GoodsFind(r *gin.Context) {
	goodsStatus := r.PostForm("goods_status")

	status, err := strconv.Atoi(goodsStatus)
	if err != nil {
		return
	}

	goods := []types.Goods{}

	global.Db.Table("goods").Where("goods_status = ?", status).First(&goods)

	r.JSON(http.StatusOK, gin.H{"goods": goods})

}
